var $dialog = (function() {
    var $dl = {
        title: 'Loading',
        text: '',
        body: '',
        id: 'dlg',
        class: 'dialog',
        icon: 'ui-icon',
        width : 'auto',
        height: 'auto',
        modal: true,
        resizable: true,
        autoOpen: false,
        dragable: false,
        dropable: false,
        closeOnEscape: false,
        object: null,
        buttons: {},
        init: function() {
        },
        set: function($option) {
            for (var $item in $option) {
                this[$item] = $option[$item];
            }

            return this;
        },
        reset: function() {
            this.title = 'Loading';
            this.text = '';
            this.body = '';
            this.id = 'dlg';
            this.class = 'dialog';
            this.icon = 'ui-icon';
            this.with = 100;
            this.height = 100;
            this.modal = true;
            this.resizable = true;
            this.autoOpen = false;
            this.dragable = false;
            this.dropable = false;
            this.closeOnEscape = false;
            this.buttons = {};
            this.object = null;
        },
        render: function($type) {
            var $htmlDlg = this.getHtmlByTypeDlg($type);
            $htmlDlg = $htmlDlg.replace(/{id}/g, this.id);
            $htmlDlg = $htmlDlg.replace(/{title}/g, this.title);
            $htmlDlg = $htmlDlg.replace(/{class}/g, this.class);

            switch ($type) {
                case 'message':
                    {
                        $htmlDlg = $htmlDlg.replace(/{icon}/g, this.icon);
                        $htmlDlg = $htmlDlg.replace(/{text}/g, this.text);
                    }
                    break;
                case 'container':
                    {
                        $htmlDlg = $htmlDlg.replace(/{body}/g, this.body);
                    }
                    break;
                default:
                    break;
            }

            this.object = $($htmlDlg);
            this.object.dialog({
                with : $dl.with ,
                height: $dl.height,
                resizable: $dl.resizable,
                autoOpen: $dl.autoOpen,
                modal: $dl.modal,
                dragable: $dl.dragable,
                dropable: $dl.dropable,
                closeOnEscape: $dl.closeOnEscape,
                buttons: $dl.buttons
            });
            return this;
        },
        getHtmlByTypeDlg: function($type) {
            $htmlMessage = '<div title="{title}" id="{id}" class="{class}">'
                    + '<p><span id="{id}_icon" class="ui-icon" style="float:left; margin:0px 7px 50px 0px;"></span>'
                    + '<span id="{id}_text">{text}</span></p></div>';

            switch ($type) {
                case 'message':
                    return $htmlMessage;
                case 'container':
                    return '<div title="{title}" id="{id}" class="{class}">{body}</div>';
                default:
                    return '';
            }
        },
        open: function() {
            if (!this.object) {
                window.alert('You are not set.');
            }

            if (!this.isOpen()) {
                this.object.dialog('open');
            }
            return this;
        },
        close: function() {
            if (!this.object) {
                window.alert('You are not set object dialog.');
            }
            this.object.dialog('close');
            return this;
        },
        destroy: function() {
            if (!this.object) {
                window.alert('You are not set object dialog.');
            }
            this.object.dialog('destroy');
            return this;
        },
        isOpen: function() {
            if (!this.object) {
                window.alert('You are not set object dialog.');
            }
            return this.object.dialog("isOpen");
        },
        confirm: function($ok, $msg, $body, $title, $cancel) {
            if (Object.isUndefined($ok) || !$.isFunction($ok)) {
                this.alert('You need defined ok handler for confirm dialog.')
                return false;
            }

            if (!Object.isString($msg) || !$msg.trim()) {
                this.alert('You need defined message for confirm dialog.');
                return false;
            }
            /////////////////////////////////
            var $option = {
                title: 'Confirm',
                text: $msg,
                icon: 'ui-icon-confirm',
                id: 'confirm_dlg',
                buttons: {
                    Ok: function() {
                        $dl.close();

                        if ($ok && $.isFunction($ok)) {
                            $ok();
                        }

                        $dl.destroy();
                    },
                    Cancel: function() {
                        $dl.close();

                        if ($cancel && $.isFunction($cancel)) {
                            $cancel();
                        }

                        $dl.destroy();
                    }
                }
            };

            if (Object.isString($title) && $title.trim()) {
                $option.title = $title;
            }

            if (Object.isString($body) && $body.trim()) {
                $option.text = $body;
            }

            this.set($option).render('message').open();
            return this;
        },
        confirmExt: function($ok, $body, $title, $cancel) {
            this.container({
                title: (!Object.isString($title) || !$title.trim()) ? 'Confirm' : $title,
                body: $body,
                id: 'confirmExt_dlg',
                buttons: {
                    Ok: function() {
                        $dl.close();
                        if ($ok && $.isFunction($ok)) {
                            $ok();
                        }
                        $dl.destroy();
                    },
                    Cancel: function() {
                        $dl.close();
                        if ($cancel && $.isFunction($cancel)) {
                            $cancel();
                        }
                        $dl.destroy();
                    }
                }
            });
        },
        alert: function($msg) {
            if (!Object.isString($msg) || !$msg.trim()) {
                window.alert('You need defined message for alert dialog.');
                return false;
            }
            ///////////////////////////////////
            var $option = {
                title: 'Alert',
                text: $msg,
                icon: 'ui-icon-alert',
                id: 'alert_dlg',
                buttons: {
                    OK: function() {
                        $dl.close().destroy();
                    }
                }
            };

            this.set($option).render('message').open();
            return this;
        },
        error: function($msg) {
            if (!Object.isString($msg) || !$msg.trim()) {
                this.alert('You need defined message for error dialog.')
                return false;
            }
            ///////////////////////////////////
            var $option = {
                title: 'Error',
                text: $msg,
                icon: 'ui-icon-error',
                id: 'alert_dlg',
                buttons: {
                    Ok: function() {
                        $dl.close().destroy();
                    }
                }
            };

            this.set($option).render('message').open();
            return this;
        },
        message: function($opt) {
            if (!Object.isObject($opt)) {
                this.alert('Param for dialog must be object in json.')
                return false;
            }

            if (!Object.isString($opt.text) || !$opt.text.trim()) {
                this.alert('You need defined message for dialog.')
                return false;
            }

            var $option = {
                title: 'Message',
                text: 'Message!',
                icon: 'ui-icon-message',
                id: 'message_dlg',
                buttons: {
                    Close: function() {
                        $dl.close().destroy();
                    }
                }
            };

            Object.extend($option, $opt)
            this.set($option).render('message').open();
            return this;
        },
        loading: function() {
            this.message({
                title: 'Loading',
                text: 'Loading... Please wait !',
                buttons: {}
            });
        },
        container: function($opt) {
            if (Object.isUndefined($opt)) {
                this.alert('You need defined param for dialog.')
                return false;
            }

            if (Object.isUndefined($opt.body)) {
                this.alert('You need defined body for dialog.')
                return false;
            }

            if (Object.isUndefined($opt.buttons)) {
                this.alert('You need defined buttons for dialog.')
                return false;
            }

            if (Object.isUndefined($opt.title)) {
                this.alert('You need defined title for dialog.')
                return false;
            }

            var $option = {
                title: 'Container',
                body: 'Message!',
                id: 'container_dlg',
                buttons: {
                    Close: function() {
                        $dl.close().destroy();
                    }
                }
            };
            
            Object.extend($option, $opt)
            this.set($option).render('container');
            this.object.dialog('option', 'width', 'auto');
            this.object.dialog('option', 'height', 'auto');
            this.open();
            return this;
        }
    };

    return $dl;
})();